package com.juneng.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.R;
import com.education.util.AliStsVideo;
import com.juneng.utils.CosApi;
import com.juneng.utils.Func;

import java.io.File;
import java.util.List;

import com.juneng.utils.Uploadutil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.juneng.entity.CourseSection;
import com.juneng.dto.CourseSectionDto;
import com.juneng.dto.CourseSectionQueryDto;
import com.juneng.vo.CourseSectionVo;
import com.juneng.service.ICourseSectionService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程章节 控制器
 *
 * @author zds
 * @since 2021-07-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("courseSection")
@Api(tags = "课程章节相关接口")
public class CourseSectionController {

	private ICourseSectionService courseSectionService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<CourseSection> detail(Integer id) {
		CourseSection detail = courseSectionService.getById(id);
		return R.success(detail);
	}

	/**
	 * 分页 课程章节
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入courseSection")
	public R<IPage<CourseSectionVo>> page(CourseSectionQueryDto dto) {
		IPage<CourseSectionVo> pages = courseSectionService.page(dto);
		return R.success(pages);
	}
	/**
	 * 列表 课程章节
	 */
	@GetMapping("/list")
	@ApiOperation(value = "不分页", notes = "传入courseSection")
	public R<List<CourseSection>> list(CourseSectionQueryDto dto) {
		List<CourseSection> list = courseSectionService.list(dto);
		return R.success(list);
	}

	/**
	 * 新增 课程章节
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入courseSection")
	public R<?> save(@RequestBody CourseSectionDto dto) {
		return R.success(courseSectionService.save(dto));
	}

	/**
	 * 修改 课程章节
	 */
	@PutMapping("/update")
	@ApiOperation(value = "修改", notes = "传入courseSection")
	public R<?> update(@RequestBody CourseSectionDto dto) {
		return R.success(courseSectionService.updateById(dto));
	}

	/**
	 * 删除 课程章节
	 */
	@DeleteMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R<?> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.success(courseSectionService.deleteLogic(Func.toIntList(ids)));
	}

	/**
	 * 视频上传
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "视频上传", notes = "传入ids")
	public R<?> addCourseSection(){
	    return R.success(courseSectionService.addCourseSection());
	}

	@PostMapping("/uploadVid")
	@ApiOperation(value = "视频上传接口", notes = "传入ids")
	@Transactional(rollbackFor = Exception.class)
	public R<?> uploadV( MultipartFile path){
		try {
			File file = CosApi.multipartFileToFile(path);
			String  videoId= Uploadutil.uploadVideo1(file);
			return R.success(videoId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.failed();
	}

	@RequestMapping(value="/voucherVid" ,method = RequestMethod.GET)
	@ApiOperation(value = "视频上传凭证接口", notes = "传入ids")
	@Transactional(rollbackFor = Exception.class)
	public R<?> voucherVid(){
		try {
			JSONObject jsonObject = AliStsVideo.assumeRole();
			return R.success(jsonObject);
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return R.failed("视频上传失败");
	}
}
